﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data.SqlClient;

using RISMOTAXDP.EmailService.MailProcess;

namespace RISMOTAXDP.EmailService
{
    /// <summary>
    /// NewsletterProcess线程
    /// </summary>
    public class MailNewsletterProcessThread : IDisposable
    {

        /// <summary>
        /// 线程对象
        /// </summary>
        private Thread m_Thread;

        private MailNewslettersProcess m_MailNewslettersProcess;


        /// <summary>
        /// 初始化
        /// </summary>
        public MailNewsletterProcessThread()
        {
            //1、创建MailNewslettersProcess
            m_MailNewslettersProcess = new MailNewslettersProcess();

            //2、创建线程
            m_Thread = new Thread(new ThreadStart(ThreadRun));

        }

        /// <summary>
        /// 启动线程
        /// </summary>
        public void Start()
        {
            //1、启动线程
            if (State == ThreadState.Stopped)
            {
                m_Thread = new Thread(new ThreadStart(ThreadRun));
            }
            if (State == ThreadState.Unstarted)
            {
                m_Thread.Start();
            }
        }


        /// <summary>
        /// 停止线程
        /// </summary>
        public void Stop()
        {
            //1、停止线程
            try
            {
                m_Thread.Interrupt();
                if (m_Thread.ThreadState != ThreadState.Stopped)
                {
                    m_Thread.Join();
                }
            }
            catch { }
        }


        /// <summary>
        /// 线程运行处理
        /// </summary>
        private void ThreadRun()
        {
            while (true)
            {
                //检查系统执行状态
                if (GlobalObject.State == ServiceState.Stopped)
                {
                    break;
                }
                switch (GlobalObject.State)
                {

                    case ServiceState.Stopped:
                        break;
                    case ServiceState.Running:

                        //创建数据库连接
                        SqlConnection conn = GlobalObject.CreateConnect();
                        if (conn != null)
                        {

                            if (GlobalObject.SystemInfo.TraceMailCreate)
                            {
                                GlobalObject.WriteSystemLog("T83000001", null);
                            }

                            m_MailNewslettersProcess.Process(conn);

                            if (GlobalObject.SystemInfo.TraceMailCreate)
                            {
                                GlobalObject.WriteSystemLog("T83000002", null);
                            }

                        }


                        //关闭数据库连接
                        GlobalObject.CloseConnect(conn);
                        break;
                    case ServiceState.Paused:

                        break;
                }

                //指定当前线程等待
                if (GlobalObject.State != ServiceState.Stopped)
                {

                    try
                    {
                        Thread.Sleep(GlobalObject.SystemInfo.NewsletterMailCheckInterval * 1000);
                    }
                    catch { }
                }




            }
        }

        /// <summary>
        /// 检查系统状态以及数据库连接状态
        /// </summary>
        /// <param name="conn">数据库连接</param>
        /// <returns>返回是否可继续</returns>
        private bool CanContinue(SqlConnection conn)
        {
            return GlobalObject.State == ServiceState.Running && conn.State != System.Data.ConnectionState.Closed;
        }

        public System.Threading.ThreadState State
        {
            get
            {
                return m_Thread.ThreadState;
            }
        }

        #region IDisposable 成员

        public void Dispose()
        {
            //1、清理线程
            m_Thread = null;

        }

        #endregion

    }
}
